package homework.Day0323;

import java.util.Iterator;

//实现Iterable接口，使用匿名内部类的方式返回一个迭代器。
// 迭代器每次next，将依次返回从范围开始的数字，直到达到范围结束的数字为止
public class Range  {
    private int start;
    private final int end;

    public Range(int start, int end) {
        this.start = start;
        this.end = end;
    }
    Iterator<Integer> iterator = new Iterator<>() {
        @Override
        public boolean hasNext() {
            return start <= end;
        }

        @Override
        public Integer next() {
            if (this.hasNext()) {
                System.out.println(start++);
            }
            return start;
        }
    };


    public static void main(String[] args) {
        Range range = new Range(1,10);
        Iterator<Integer> ran = range.iterator;
        while (ran.hasNext()) {
            ran.next();
        }
    }
}
